import request from '../utils/request'

// 预约记录类型定义
export interface Appointment {
  appointmentId: number
  userId: number
  doctorId: number
  timelistId: number
  status: number  // 预约状态：
                  // 0-预约已完成
                  // 1-正在申请(未通过)
                  // 2-申请通过
                  // 3-预约申请通过，咨询师申请取消，咨询者未回复
                  // 4-预约申请通过，咨询者申请取消，咨询师未回复
                  // 5-预约申请通过，咨询师申请取消，咨询者同意取消
                  // 6-预约申请通过，咨询师申请取消，咨询者不同意取消
                  // 7-预约申请通过，咨询者申请取消，咨询师不同意取消
                  // 8-预约申请通过，咨询者申请取消，咨询师同意取消
  createTime: string
  weekday: number
  begTime: string
  endTime: string
  userName?: string
  doctorName?: string
}

// 创建预约的请求数据类型
export interface CreateAppointmentRequest {
  clientId: number    // 用户ID
  doctorId: number    // 医生ID
  timelistId: number  // 时间段ID
}

// 创建预约
export const createAppointment = (data: CreateAppointmentRequest) => {
  return request({
    url: '/appointment/create',
    method: 'post',
    params: data
  })
}

// 获取用户的所有预约记录
export const getUserAppointments = (userId: number) => {
  return request<Appointment[]>({
    url: '/appointment/user/list',
    method: 'get',
    params: { userId }
  })
}
// 获取咨询师可预约时间段
export const getDoctorSchedule = (doctorId: number) => {
  return request({
    url: '/timelist/doctor/list',
    method: 'get',
    params: { doctorId }
  })
}

// 获取用户的待确认预约
export const getUserPendingAppointments = (userId: number) => {
  return request<Appointment[]>({
    url: '/appointment/user/pending',
    method: 'get',
    params: { userId }
  })
}

// 获取用户的已确认预约
export const getUserConfirmedAppointments = (userId: number) => {
  return request<Appointment[]>({
    url: '/appointment/user/confirmed',
    method: 'get',
    params: { userId }
  })
}

// 用户发起取消预约
export const userInitiateCancelAppointment = (appointmentId: number) => {
  return request({
    url: '/appointment/user/initiate-cancel',
    method: 'put',
    params: { appointmentId }
  })
}

// 咨询师发起取消预约
export const doctorInitiateCancelAppointment = (appointmentId: number) => {
  return request({
    url: '/appointment/doctor/initiate-cancel',
    method: 'put',
    params: { appointmentId }
  })
}

// 用户同意取消预约
export const userAcceptCancel = (appointmentId: number) => {
  return request({
    url: '/appointment/user/confirm-cancel',
    method: 'put',
    params: { appointmentId }
  })
}

// 用户拒绝取消预约
export const userRejectCancel = (appointmentId: number) => {
  return request({
    url: '/appointment/user/reject-cancel',
    method: 'put',
    params: { appointmentId }
  })
}

// 咨询师同意取消预约
export const doctorAcceptCancel = (appointmentId: number) => {
  return request({
    url: '/appointment/doctor/confirm-cancel',
    method: 'put',
    params: { appointmentId }
  })
}

// 咨询师拒绝取消预约
export const doctorRejectCancel = (appointmentId: number) => {
  return request({
    url: '/appointment/doctor/reject-cancel',
    method: 'put',
    params: { appointmentId }
  })
}

// 获取咨询师的所有预约记录
export const getDoctorAppointments = (doctorId: number) => {
  return request<Appointment[]>({
    url: '/appointment/doctor/list',
    method: 'get',
    params: { doctorId }
  })
}

// 获取咨询师的待处理预约
export const getDoctorPendingAppointments = (doctorId: number) => {
  return request<Appointment[]>({
    url: '/appointment/doctor/pending',
    method: 'get',
    params: { doctorId }
  })
}

// 确认预约
export const confirmAppointment = (appointmentId: number) => {
  return request({
    url: '/appointment/confirm',
    method: 'put',
    params: { appointmentId }
  })
}

// 拒绝预约
export const rejectAppointment = (appointmentId: number) => {
  return request({
    url: '/appointment/reject',
    method: 'put',
    params: { appointmentId }
  })
}

// 完成预约
export const completeAppointment = (appointmentId: number) => {
  return request({
    url: '/appointment/complete',
    method: 'put',
    params: { appointmentId }
  })
}

// 获取预约详情
export const getAppointmentDetail = (appointmentId: number) => {
  return request<Appointment>({
    url: `/appointment/get/${appointmentId}`,
    method: 'get'
  })
}

// 添加获取筛选后的医生列表接口
export const getDoctorByFilter = (params: {
  name?: string
  sex?: number
  skill?: string
}) => {
  return request<Doctor[]>({
    url: '/doctor/filter',
    method: 'get',
    params
  })
}

// 时间段类型定义
export interface Timelist {
  timelistId: number
  doctorId: number
  weekday: number
  begTime: string
  endTime: string
  maxCapacity: number
  currentCapacity: number
  isActive: number
}
